package com.ruoyi.grade.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.grade.domain.entity.StudentTotalGrade;
import com.ruoyi.grade.mapper.StudentTotalGradeMapper;
import com.ruoyi.grade.service.IStudentTotalGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentTotalGradeServiceImpl extends ServiceImpl<StudentTotalGradeMapper, StudentTotalGrade>
    implements IStudentTotalGradeService {

    @Autowired
    private StudentTotalGradeMapper studentTotalGradeMapper;

    @Override
    public String importUser(List<StudentTotalGrade> studentTotalGradeList, Boolean updateSupport) {
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (StudentTotalGrade studentTotalGrade : studentTotalGradeList) {
            if(!checkStudentTotalGradeExist(studentTotalGrade.getStudentId(), studentTotalGrade.getAcademicYear(), studentTotalGrade.getSemester())){
                //总成绩不存在
                //调用保存业务
                studentTotalGradeMapper.insert(studentTotalGrade);

                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " + studentTotalGrade.getStudentId() + " 导入成功");
            } else if (updateSupport){
                //总成绩存在
                //调用修改业务业务
                studentTotalGrade.setId(getStudentTotalGradeIdByStudentId(studentTotalGrade.getStudentId(), studentTotalGrade.getAcademicYear(), studentTotalGrade.getSemester()));
                studentTotalGradeMapper.updateById(studentTotalGrade);

                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " +  studentTotalGrade.getStudentId() + " 导入成功");
            } else{
                //账号存在且不支持更新
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、账号 " + studentTotalGrade.getStudentId() + " 已存在");
            }

        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }

        return failureNum == 0? failureMsg.toString(): successMsg.toString();
    }

    @Override
    public Boolean checkStudentTotalGradeExist(String studentId, String academicYear, String semester) {
        //设置查询条件
        LambdaQueryWrapper<StudentTotalGrade> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StudentTotalGrade::getStudentId, studentId);
        lqw.eq(StudentTotalGrade::getAcademicYear, academicYear);
        lqw.eq(StudentTotalGrade::getSemester, semester);
        //调用查询业务
        return studentTotalGradeMapper.selectOne(lqw) != null;
    }

    @Override
    public Long getStudentTotalGradeIdByStudentId(String studentId, String academicYear, String semester) {
        //设置查询条件
        LambdaQueryWrapper<StudentTotalGrade> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StudentTotalGrade::getStudentId, studentId);
        lqw.eq(StudentTotalGrade::getAcademicYear, academicYear);
        lqw.eq(StudentTotalGrade::getSemester, semester);
        //调用查询业务
        return studentTotalGradeMapper.selectOne(lqw).getId();
    }
}
